package com.admin.education.services.Implements;

import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.userRecord.Mapper.UserRecordMapper;
import com.admin.education.dataTransferObject.userRecord.Request.QueryUserRecordRequest;
import com.admin.education.dataTransferObject.userRecord.UserRecordTransfer;
import com.admin.education.models.QUserInformation;
import com.admin.education.models.QUserRecords;
import com.admin.education.models.UserRecords;
import com.admin.education.repositories.UserRecordRepository;
import com.admin.education.services.Interfaces.IUserRecordService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.List;
@Service
public class UserRecordService implements IUserRecordService {
    private QUserRecords qUserRecords;
    private QUserInformation qUserInformation;
    public UserRecordService(){
        qUserRecords =QUserRecords.userRecords;
        qUserInformation =QUserInformation.userInformation;
    }
    @Autowired
    private UserRecordRepository userRecordRepository;
    @Autowired
    private UserRecordMapper userRecordMapper;

    @Override
    public ServicesExcuteResult<PageResponse<UserRecordTransfer>> queryUserRecord(@Valid QueryUserRecordRequest queryUserRecordRequest) {
        ServicesExcuteResult<PageResponse<UserRecordTransfer>> servicesExcuteResult=new ServicesExcuteResult<>();

        JPAQuery<UserRecords> userRecordsJPAQuery = this.userRecordRepository.selectFrom(qUserRecords);

        if(null!=queryUserRecordRequest.getClassesId()){
            userRecordsJPAQuery =userRecordsJPAQuery.where(qUserRecords.userInformation.classes.id.eq(queryUserRecordRequest.getClassesId()));
        }
        if(null!=queryUserRecordRequest.getSchoolsId()){
            userRecordsJPAQuery =userRecordsJPAQuery.where(qUserRecords.userInformation.schools.id.eq(queryUserRecordRequest.getClassesId()));
        }
        if(null!=queryUserRecordRequest.getGradeId()){
            userRecordsJPAQuery = userRecordsJPAQuery.innerJoin(qUserRecords.userInformation,qUserInformation).where(qUserInformation.classes.grades.id.eq(queryUserRecordRequest.getGradeId()));
        }
        long totalCount = userRecordsJPAQuery.fetchCount();

        userRecordsJPAQuery = userRecordsJPAQuery.orderBy(qUserRecords.createTime.desc()).offset((queryUserRecordRequest.getPageIndex()-1)*queryUserRecordRequest.getPageSize()).limit(queryUserRecordRequest.getPageSize());

        List<UserRecords> userRecords= userRecordsJPAQuery.fetch();

        List<UserRecordTransfer> userRecordTransfers = this.userRecordMapper.mapFrom(userRecords);

        PageResponse<UserRecordTransfer> activityTransferPageResponse = new PageResponse<>(queryUserRecordRequest.getPageSize(),queryUserRecordRequest.getPageIndex(),(int)totalCount,userRecordTransfers);

        servicesExcuteResult.setData(activityTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }
}
